package model.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import vo.MemberVO;
import vo.ProductVO;
import vo.VendorVO;

import com.ibatis.sqlmap.client.SqlMapClient;

public class ProductDAO {
	private SqlMapClient client;
	 
	private static ProductDAO pdao = new ProductDAO();
	private ProductDAO(){
		SqlMapFactory sf = SqlMapFactory.getInstance();
		client=sf.getClient();
	}
	
	public static ProductDAO getInstance()
	{
		if(pdao==null) pdao=new ProductDAO(); return pdao;
	}
	
	public Object InsertProduct(ProductVO pvo)throws SQLException{
		Object obj=null;
		try{
		obj= client.insert("product.InsertProduct", pvo);
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return obj;
	}
	public int selectTotalProductName(String id, int pageNo, String productName){
		int totalcnt = 0;
		try {
			HashMap map = new HashMap();
			map.put("id", id);
			map.put("productName", productName)	;
			totalcnt = (Integer) client.queryForObject("product.SelectTotalCountName", map);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return totalcnt;
	}
	public int selectTotalProduct(String id){
		int totalCnt = 0;
		try {
			totalCnt = (Integer) client.queryForObject("product.SelectTotalCount",id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return totalCnt;
				
	}
	public ProductVO SelectProductById(String id, String productId) throws SQLException{
		
		ProductVO pvo= null; 
		try{
			HashMap map = new HashMap();
			map.put("id", id);
			map.put("productId", productId);
			pvo=(ProductVO) client.queryForObject("product.SelectProductById", map);
		}catch(SQLException e){
			e.printStackTrace();
		}
		return pvo;
	}
	public ArrayList<ProductVO> SelectProductByName(String id, int pageNo, String productName) throws SQLException{
		ArrayList<ProductVO> list = new ArrayList<ProductVO>();
		try{
			HashMap map = new HashMap();
			map.put("id", id);
			map.put("productName", productName);
			map.put("pageNo", pageNo);
			list =  (ArrayList<ProductVO>) client.queryForList("product.SelectProductByName", map);
		}catch(SQLException e){
			e.printStackTrace();
		}
		return list;
	}
	public ArrayList<ProductVO> SelectAllProduct(String id, int pageNo){
		ArrayList<ProductVO> list=null;
		try {
			HashMap map = new HashMap();
			map.put("id", id);
			map.put("pageNo", pageNo);
			list = (ArrayList<ProductVO>)client.queryForList("product.SelectAllProduct",map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public int UpdateProduct(ProductVO pvo)throws SQLException{
		int cnt = (Integer)client.update("product.UpdateProduct",pvo);
		return cnt;
	}
	public int DeleteProduct(ProductVO pvo)throws Exception{
		int cnt = (Integer)client.update("product.DeleteProduct",pvo);
		return cnt;
	}
}